package com.person.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
  * 56.合并区间
  * 2022-11-08 11:35:39
**/
public class Sequence_0056_MergeIntervals {
    public static void main(String[] args) {
        Solution solution = new Sequence_0056_MergeIntervals().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[][] merge(int[][] intervals) {
        //
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        ArrayList<int[]> mergeList = new ArrayList<>();
        mergeList.add(intervals[0]);
        for (int i = 1; i < intervals.length; i++) {
            int[] arr = intervals[i];
            if (mergeList.get(mergeList.size() - 1)[1] >= arr[0]){
                mergeList.get(mergeList.size() - 1)[1] = Math.max(arr[1],mergeList.get(mergeList.size() - 1)[1]);
            }else{
                mergeList.add(arr);
            }
        }

        return mergeList.toArray(new int[mergeList.size()][]);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}